﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Verkefni5.Models;

namespace Verkefni5.Controllers
{
    [Authorize]
    public class LecturesController : ApiController
    {
       
        // GET api/lectures
         
        public IEnumerable<Lecture> Get()
        {
            AppDataContext db = new AppDataContext();
            return db.Lectures;
        }

        // GET api/lectures/5
        
        public Lecture Get(int id)
        {

            AppDataContext db = new AppDataContext();
            var l = (from lecture in db.Lectures
                           where lecture.ID == id
                           select lecture).SingleOrDefault();
            if (l == null)
            {
                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.NotFound,
                    Content = new StringContent("Lecture not found")
                });
            }
            return l;
        }
        

        // POST api/lectures
        [Authorize(Roles = "Teacher")]  
        public void Post(Lecture value)
        {

            AppDataContext db = new AppDataContext();
            Lecture l = new Lecture();
            l.Name = value.Name;
            l.LectureURL = value.LectureURL;
            l.DatePublished = DateTime.Now.ToString();
            l.Description = value.Description;
            db.Lectures.Add(l);
            db.SaveChanges();

        }

        // PUT api/lectures/5
        [Authorize(Roles = "Teacher")]
        public void Put(int id, Lecture value)
        {

            AppDataContext db = new AppDataContext();

            var l = (from lecture in db.Lectures
                     where lecture.ID == id
                     select lecture).SingleOrDefault();

            l.Description = value.Description;
            l.LectureURL = value.LectureURL;
            l.Name = value.Name;
            db.SaveChanges();

        }

        // DELETE api/lectures/5
        public void Delete(int id)
        {
        }

       
    }
}
